﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using zichan.DAL;
using zichan.Entity;
using zichan.Entity.View;
using MvcPaging;
using System.Data.SqlClient;

namespace zichan.BLL
{
    public class DepartmentRoleService
    {
        private delegate void Creattable();
        public static IPagedList<Department> List(int pageIndex,int pageSize)
        {
            using (ZichanDBContext db = new ZichanDBContext())
            {
                if (db.Departments.Count() <= 0)
                {
                    Creattable create = delegate ()
                  {
                      List<string> departmentStringList = db.Staffs.Select(o => o.DepartmentName).Distinct().ToList();
                      List<Department> list = new List<Department>();
                      foreach (string s in departmentStringList)
                      {
                          Department model = new Department();
                          model.DepartmentName = s;
                          list.Add(model);
                      }
                      db.Departments.AddRange(list);
                      db.SaveChanges();
                  };
                    create.Invoke();
                }
                List<Department> departmentList = db.Departments.ToList();
                var roles = db.Roles.Select(o => new { o.Id, o.Name });
                foreach(Department d in departmentList)
                {
                    //if(db.DepartmentRoles.Where(o => o.DepartmentId == d.Id).Select(o=>o.RoleId).Count()>0)
                    //{
                        d.RoleName =string.Join(",", db.DepartmentRoles.Where(o=>o.DepartmentId==d.Id).Select(o=>o.RoleName).ToList());
                    //}
                }
                return departmentList.ToPagedList(pageIndex-1,pageSize,departmentList.Count());
            }
        }
        public static string Add(DepartmentRole model,string constr)
        {
            using (ZichanDBContext db = new ZichanDBContext())
            {
                db.DepartmentRoles.Add(model);
                db.SaveChanges();
                model.DepartmentName = db.Departments.FirstOrDefault(o => o.Id == model.DepartmentId).DepartmentName;
                SqlConnection sqlconn = new SqlConnection(constr);
                SqlCommand sqlcom = new SqlCommand("Pro_CreateAuth", sqlconn);
                sqlcom.CommandType = System.Data.CommandType.StoredProcedure;
                sqlconn.Open();
                SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@departmentName", model.DepartmentName), new SqlParameter("@roleId", model.RoleId) };
                sqlcom.Parameters.AddRange(paras);
                int err = sqlcom.ExecuteNonQuery();
                if (err == -1)
                {
                    return "授权失败";
                }
                else
                {
                    return string.Empty;
                }
            }
        }
        public static DepartmentRole Get(int id)
        {
            using (ZichanDBContext db = new ZichanDBContext())
            {
                return db.DepartmentRoles.FirstOrDefault(o => o.Id == id);
            }
        }
        public static List<DepartmentRole> DepartmentRoleList(int departmentId,int pageIndex,int pageSize)
        {
            using (ZichanDBContext db = new ZichanDBContext())
            {
                List<DepartmentRole> list = db.DepartmentRoles.Where(o => o.DepartmentId == departmentId).ToList();
                return list;
            }
        }
        public static List<int> DepartmentRoleList(int id)
        {
            using (ZichanDBContext db = new ZichanDBContext())
            {
                List<int> list = db.DepartmentRoles.Where(o => o.DepartmentId == id).Select(o => o.RoleId).ToList();
                return list;
            }
        }
        public static string Delete(int departmentId, int roleId, int id, string constr)
        {
            using (ZichanDBContext db = new ZichanDBContext())
            {
                DepartmentRole model = db.DepartmentRoles.FirstOrDefault(o => o.Id == id);
                string departmentName = db.Departments.FirstOrDefault(o => o.Id == departmentId).DepartmentName;
                db.DepartmentRoles.Remove(model);
                db.SaveChanges();
                SqlConnection sqlcon = new SqlConnection(constr);
                SqlCommand comm = new SqlCommand("Pro_DeleteAuth", sqlcon);
                comm.CommandType = System.Data.CommandType.StoredProcedure;
                sqlcon.Open();
                SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@departmentName", departmentName), new SqlParameter("@roleId", roleId) };
                comm.Parameters.AddRange(paras);
                int err = comm.ExecuteNonQuery();
                if (err == -1)
                {
                    return "删除失败";
                }
                else
                {
                    return string.Empty;
                }
            }
        }
    }
}
